package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.NursingLevelMapper;
import com.itheima.pojo.NursingLevel;
import com.itheima.pojo.NursingProject;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.ProjectParam;
import com.itheima.service.NursingLevelService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;

@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public PageResult getAll(ProjectParam projectParam) {
        //封装分页信息
        IPage<NursingLevel> page = new Page<>(projectParam.getPage(),projectParam.getPageSize());
        //封装查询条件
        QueryWrapper<NursingLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(projectParam.getName()),"name",projectParam.getName())
                .like(ObjectUtils.isNotEmpty(projectParam.getStatus()),"status",projectParam.getStatus());
        //分页查询
        IPage<NursingLevel> pageInfo = nursingLevelMapper.selectPage(page,queryWrapper);
        return new PageResult(pageInfo.getTotal(),pageInfo.getRecords());
    }

    @Override
    public void addLevel(NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public NursingLevel getLevelById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    @Override
    public void updateLevel(NursingLevel nursingLevel) {
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevelMapper.updateById(nursingLevel);
    }

    @Override
    public void deleteLevel(Long id) {
        nursingLevelMapper.deleteById(id);
    }
}
